
package com.xiaoxiong.mami.http;


import com.alibaba.fastjson.JSONObject;
import springfox.documentation.spring.web.json.Json;

/**
 * 接口调用返回结果
 *
 * @author leki
 */
public class AppJsonResult {

    private String code;
    private String msg;
    private JSONObject data;

    public AppJsonResult() {
        super();
    }

    public AppJsonResult(String code, String msg, JSONObject data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 业务处理成功时，构造处理成功的接口返回结果
     *
     * @param data - JSONObject
     * @return
     */
//    public static AppJsonResult createSuccessResult(JSONObject data) {
//
//        if (null == data) {
//            data = JSONObject.parseObject("{}");
//        }
//
//        AppJsonResult result = new AppJsonResult(ResultCode.SUCCESS_CODE, "", data);
//        return result;
//    }

    public static JSONObject createSuccessResultStr(JSONObject data) {

        if (null == data) {
            data = JSONObject.parseObject("{}");
        }

        AppJsonResult result = new AppJsonResult(ResultCode.SUCCESS_CODE, "", data);
        return result.toResult();
    }


    public static JSONObject createSuccessResultMsg(String msg) {

        JSONObject data = JSONObject.parseObject("{}");

        AppJsonResult result = new AppJsonResult(ResultCode.SUCCESS_CODE, msg, data);
        return result.toResult();
    }

    public static JSONObject createSuccessResultWithMsg(JSONObject data, String msg) {

        if (null == data) {
            data = JSONObject.parseObject("{}");
        }

        AppJsonResult result = new AppJsonResult(ResultCode.SUCCESS_CODE, msg, data);
        return result.toResult();
    }

    public static JSONObject createErrorResultStr(String msg) {

        AppJsonResult result = new AppJsonResult(ResultCode.ERROR_CODE, msg, null);
        return result.toResult();
    }

    /**
     * 业务处理失败或者出现异常时，构造处理失败的接口返回结果
     *
     * @param msg
     * @return
     */
//    public static AppJsonResult createErrorResult(String msg) {
//        AppJsonResult result = new AppJsonResult(ResultCode.ERROR_CODE, msg, null);
//        return result;
//    }

    /**
     * token校验失败时，构造处理token失效的接口返回结果
     *
     * @param msg
     * @return
     */
    public static JSONObject createTokenInvalidResult(String msg) {
        AppJsonResult result = new AppJsonResult(ResultCode.TOKEN_INVALID_CODE, msg, null);
        return result.toResult();
    }

//    public static AppJsonResult createConnectTimeOutResult(String msg) {
//        AppJsonResult result = new AppJsonResult(ResultCode.TOKEN_CONNECT_TIMEOUT_CODE, msg, null);
//        return result;
//    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public JSONObject getData() {
        return data;
    }

    public void setData(JSONObject data) {
        this.data = data;
    }


    public JSONObject toResult() {
        JSONObject json = new JSONObject();
        json.put("code", this.getCode());
        json.put("msg", this.getMsg());
        json.put("data", this.getData());
        return json;
    }


}
